package com.android.svg.support.writer.impl;


import com.android.svg.support.vector.model.Vector;
import com.android.svg.support.render.VectorRenderer;
import com.android.svg.support.utils.Dimen;
import com.android.svg.support.writer.JavaClassWriter;

import java.io.BufferedWriter;
import java.io.IOException;

public class SVGRendererTemplateWriter extends JavaClassWriter {

    private VectorRenderer mRenderer;
    private Vector mVector;

    public SVGRendererTemplateWriter(VectorRenderer renderer, Vector vector) {
        this.mRenderer = renderer;
        this.mVector = vector;
        setClassRelation("SVGRenderer", null);
    }

    @Override
    protected void writeImports(BufferedWriter bw) throws IOException {
        super.writeImports(bw);
        bw.write("import android.content.Context;");
        bw.newLine();
        bw.write("import android.graphics.Canvas;");
        bw.newLine();
        bw.write("import android.graphics.ColorFilter;");
        bw.newLine();
        bw.write("import android.graphics.Paint;");
        bw.newLine();
        bw.newLine();
        bw.write("import com.android.svg.support.SVGRenderer;");
        bw.newLine();
        bw.newLine();
    }

    @Override
    protected void writeClassComment(BufferedWriter bw) throws IOException {
        super.writeClassComment(bw);
        bw.write("/**");
        bw.newLine();
        bw.write(" * AUTO-GENERATED FILE.  DO NOT MODIFY.");
        bw.newLine();
        bw.write(" * ");
        bw.newLine();
        bw.write(" * This class was automatically generated by the");
        bw.newLine();
        bw.write(" * SVG-Generator. It should not be modified by hand.");
        bw.newLine();
        bw.write(" */");
    }

    @Override
    protected void writeFields(BufferedWriter bw) throws IOException {
    }

    @Override
    protected void writeConstructMethods(BufferedWriter bw) throws IOException {
        bw.newLine();
        bw.write(HEAD_SPACE + "public " + mClassSimpleName + "(Context context) {");
        bw.newLine();
        bw.write(HEAD_SPACE + HEAD_SPACE + "super(context);");
        bw.newLine();
        bw.write(HEAD_SPACE + HEAD_SPACE + "mAlpha = " + mVector.alpha + "f;");
        bw.newLine();
        bw.write(HEAD_SPACE + HEAD_SPACE + "mWidth = " + getDimenSize(mVector.width) + ";");
        bw.newLine();
        bw.write(HEAD_SPACE + HEAD_SPACE + "mHeight = " + getDimenSize(mVector.height) + ";");
        bw.newLine();
        bw.write(HEAD_SPACE + "}");
        bw.newLine();
    }

    @Override
    protected void writeMethods(BufferedWriter bw) throws IOException {
        writeMethodRender(bw);
    }

    private void writeMethodRender(BufferedWriter bw) throws IOException {
        bw.newLine();
        bw.write(HEAD_SPACE + "@Override");
        bw.newLine();
        bw.write(HEAD_SPACE + "public void render(Canvas canvas, int w, int h, ColorFilter filter) {");
        bw.newLine();
        bw.write(mRenderer.renderResult());
        bw.newLine();
        bw.write(HEAD_SPACE + "}");
        bw.newLine();
    }

    private String getDimenSize(String size) {
        if (Dimen.isDip(size)) {
            return "dip2px(" + Dimen.convert(size) + "f)";
        } else if (Dimen.isPx(size)) {
            return "(int)" + Dimen.convert(size) + "f";
        } else {
            return "0f";
        }
    }
}
